package com.fqgj.sentry.filter;


import com.fqgj.sentry.common.utils.HttpHelper;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;

/**
 * Created by lpp on 15/2/7.
 */
public class HttpServletRequestBodyWrapper extends javax.servlet.http.HttpServletRequestWrapper {

    private final byte[] body;

    public HttpServletRequestBodyWrapper(HttpServletRequest request) throws IOException {
        super(request);
        body = HttpHelper.getBodyString(request).getBytes(Charset.forName("UTF-8"));
    }

    @Override
    public BufferedReader getReader() throws IOException {
        return new BufferedReader(new InputStreamReader(getInputStream()));
    }

//    @Override
//    public ServletInputStream getInputStream() throws IOException {
//
//        final ByteArrayInputStream bais = new ByteArrayInputStream(body);
//
//        return new ServletInputStream() {
//
//            @Override
//            public int read() throws IOException {
//                return bais.read();
//            }
//
//            @Override
//            public boolean isFinished() {
//                return false;
//            }
//
//            @Override
//            public boolean isReady() {
//                return false;
//            }
//
//            @Override
//            public void setReadListener(ReadListener readListener) {
//
//            }
//        };
//    }


}